/*
 * Copyright (c) 2020-2099 the original author or authors. All rights reserve.
 */
package org.tianyun.cloud.config.console.property;

import org.tianyun.cloud.config.console.sources.NameWildcardPropertySource;

import java.util.HashMap;
import java.util.Map;

/**
 * 命名空间，存储控制台下发的属性源
 *
 * @author ebert_chan
 */
public class Namespace {

    private final String name;

    private final Map<String, NameWildcardPropertySource> propertySources;

    public Namespace(String name) {
        this.name = name;
        this.propertySources = new HashMap<>();
    }

    /**
     * 返回命名空间的名称
     */
    public String getName() {
        return name;
    }

    /**
     * 保存或替换属性源
     *
     * @param nameWildcardPropertySource
     */
    public void putPropertySource(NameWildcardPropertySource nameWildcardPropertySource) {
        synchronized (propertySources) {
            propertySources.put(nameWildcardPropertySource.getName(), nameWildcardPropertySource);
        }
    }

    /**
     * 删除属性源
     *
     * @param name
     */
    public void removeProertySource(String name) {
        synchronized (propertySources) {
            propertySources.remove(name);
        }
    }

    /**
     * 判断属性源是否存在
     *
     * @param name
     * @return boolean
     */
    public boolean containsPropertySource(String name) {
        return propertySources.containsKey(name);
    }

    /**
     * 根据名称获取属性源
     * <p>
     *
     * @param name
     * @return org.tianyun.cloud.config.env.property.source.NameWildcardPropertySource
     */
    public NameWildcardPropertySource getPropertySource(String name) {
        return propertySources.get(name);
    }

    public Map<String, NameWildcardPropertySource> getPropertySources() {
        return propertySources;
    }

}
